package com.lbn.jfqsc.app.recruitment.apply;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputEditText;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.lbn.baseframe.base.BaseActivity;
import com.lbn.baseframe.ui.NAppbar;
import com.lbn.baseframe.utils.EasyToast;
import com.lbn.jfqsc.R;
import com.lypeer.fcpermission.FcPermissions;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ApplyActivity extends BaseActivity<ApplyPresenter> implements IApplyView {

    private final int REQUEST_CODE = 1000;

    @BindView(R.id.select_file)
    Button selectFile;
    @BindView(R.id.user_name)
    TextInputEditText userName;
    @BindView(R.id.textInputEditText)
    TextInputEditText textInputEditText;
    @BindView(R.id.specialty_tie)
    TextInputEditText specialtyTie;
    @BindView(R.id.work_time_tie)
    TextInputEditText workTimeTie;
    @BindView(R.id.skills_tie)
    TextInputEditText skillsTie;
    @BindView(R.id.introduce_tie)
    TextInputEditText introduceTie;
    @BindView(R.id.activity_apply)
    RelativeLayout activityApply;
    @BindView(R.id.submit_bt)
    Button submitBt;
    private Uri fileUri = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply);
        ButterKnife.bind(this);
        setAppbar(new NAppbar(this));
        appbar.setTitle("申请");
        appbar.openBack();
    }


    @Override
    public ApplyPresenter createPresenter() {
        return new ApplyPresenter();
    }


    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, ApplyActivity.class));
    }


    @NonNull
    @Override
    protected String getRationale4NeverAskAgain() {
        return "上传文件，请先打开相应权限";
    }

    @Override
    public void onPermissionDenied(int requestCode, List<String> perms) {
//        FcPermissions.checkDeniedPermissionsNeverAskAgain(this, perms);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        if (perms.size() < 2)
            return;
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("*/*");//设置类型，我这里是任意类型，任意后缀的可以这样写。
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, REQUEST_CODE);
    }

    private void requestCameraPermission() {
        FcPermissions.requestPermissions(this,
                "即将请求访问文件的权限，是否立即前往",
                FcPermissions.REQ_PER_CODE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }


    @OnClick({R.id.select_file, R.id.submit_bt})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.select_file:
                requestCameraPermission();
                break;
            case R.id.submit_bt:

                if (checkData()) {
                    mPresenter.apply(this);
                }
                break;
        }
    }

    private boolean checkData() {
        if (TextUtils.isEmpty(userName.getText().toString().trim())) {
            EasyToast.showShort(this, "请输入姓名");
        }
        if (TextUtils.isEmpty(textInputEditText.getText().toString().trim())) {
            EasyToast.showShort(this, "请输入联系方式");
        }
        if (TextUtils.isEmpty(specialtyTie.getText().toString().trim())) {
            EasyToast.showShort(this, "请输入特长");
        }
        if (TextUtils.isEmpty(workTimeTie.getText().toString().trim())) {
            EasyToast.showShort(this, "请输入工作时间");
        }
        if (TextUtils.isEmpty(skillsTie.getText().toString().trim())) {
            EasyToast.showShort(this, "请输入拥有技能");
        }
        if (TextUtils.isEmpty(introduceTie.getText().toString().trim())) {
            EasyToast.showShort(this, "请输入自我介绍");
        }
        return true;
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == REQUEST_CODE) {
                fileUri = data.getData();
                String path = fileUri.getPath().toString();
                String fileName = new File(path).getName();
                selectFile.setText(fileName);
            }
        }
    }
}
